package cn.lena.idle;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import cn.lena.idle.dao.CatagoryDbHelper;
import cn.lena.idle.dao.GoodsDbHelper;
import cn.lena.idle.pojo.DTO.CurrentGoods;
import cn.lena.idle.pojo.DTO.LoginUser;
import cn.lena.idle.pojo.DTO.Version;
import cn.lena.idle.pojo.Goods;

public class GoodsDetailActivity2 extends AppCompatActivity {

    private ImageView back,photo;
    private Button buy;
    private TextView title,describe,catagory,price,phone,address,time;
    private Goods goods;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_detail);

        back=(ImageView)findViewById(R.id.rb3);
        buy=(Button)findViewById(R.id.buy);
        photo=(ImageView)findViewById(R.id.goods_photo);
        title=(TextView)findViewById(R.id.goods_title);
        describe=(TextView)findViewById(R.id.goods_describe);
        catagory=(TextView)findViewById(R.id.goods_catagory);
        price=(TextView)findViewById(R.id.goods_price);
        phone=(TextView)findViewById(R.id.goods_phone);
        address=(TextView)findViewById(R.id.goods_address);
        time=(TextView)findViewById(R.id.goods_time);
        goods= CurrentGoods.SELECT;

        // 修改按钮不可见
        buy.setVisibility(View.INVISIBLE);

        // 初始化页面
        photo.setImageBitmap(BitmapFactory.decodeByteArray(goods.getPicture(),0,goods.getPicture().length));
        title.setText(goods.getGoodsName());
        describe.setText(goods.getDescribe());
        catagory.setText(getCatagorynameById(goods.getCatagoryId()));
        price.setText(goods.getPrice());
        phone.setText(goods.getPhone());
        address.setText(goods.getAddress());
        time.setText(goods.getPublishTime());

        // 购买商品
        buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buy(goods);
            }
        });

        //返回上一页
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(GoodsDetailActivity2.this,IndexActivity.class);
                startActivity(intent);
            }
        });

    }

    /**
     * 购买商品
     * @param goods
     */
    private void buy(Goods goods){
        GoodsDbHelper goods_db=new GoodsDbHelper(getApplicationContext(),GoodsDbHelper.DB_NAME,null,Version.GOODS);
        if(goods.getUserId() == LoginUser.CURRENT_USER.getId()){
            Toast.makeText(GoodsDetailActivity2.this,"不能购买自己发布的商品",Toast.LENGTH_SHORT).show();
            return;
        }
        int result=goods_db.buyGoods(goods, LoginUser.CURRENT_USER);
        if(result == 1){
            Toast.makeText(GoodsDetailActivity2.this,"购买成功，请尽早取货！",Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(GoodsDetailActivity2.this,IndexActivity.class);
            startActivity(intent);
        }else{
            Toast.makeText(GoodsDetailActivity2.this,"购买失败！",Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 根据分类id查询分类名称
     */
    private String getCatagorynameById(Integer id){
        CatagoryDbHelper catagory_db=new CatagoryDbHelper(getApplicationContext(), CatagoryDbHelper.DB_NAME,null, Version.CATAGORY);
        return catagory_db.getById(id);
    }


}
